package loveappservice.dao.impl;


import loveappservice.dao.User_dao;
import loveappservice.domain.*;
import loveappservice.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class User_dao_impl implements User_dao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /*通过用户ID查询一个用户*/
    @Override
    public Users finOneUser(int user_id) {
        String sql = "select * from users where user_id=?";
        Users users = template.queryForObject(sql, new BeanPropertyRowMapper<Users>(Users.class), user_id);
        System.out.println("查询了数据");
        return users;
    }

    /*查询了所有纪念日*/
    @Override
    public List<Comm> allComm() {
        String sql = "select * from commemoration";
        List<Comm> comms = template.query(sql, new BeanPropertyRowMapper<Comm>(Comm.class));
        System.out.println("查询了所有纪念日");
        return comms;
    }

    /*查询纪念日是否已存在*/
    @Override
    public int selectCommExist(String comm_name) {
        String sql = "select count(comm_name) from commemoration where comm_name=?";
        Long totlal =  template.queryForObject(sql,Long.class,comm_name);
        System.out.println("查询了有无纪念日");
        if (totlal==0) {
            return 1;
        }else {
            return 0;
        }
    }
    /*新增一个纪念日*/
    @Override
    public int addComm(Comm comm) {
        String sql = "insert into commemoration value(null,?,?,?,?)";
        int req = template.update(sql,comm.getComm_name(),comm.getComm_time(),comm.getTime_type(),comm.getComm_type());
        System.out.println("新增了一个纪念日");
        return req;
    }

    /*新增一个纪念日的信息*/
    @Override
    public Comm findOneComm(int comm_id) {
        String sql = "select * from commemoration where comm_id=?";
        Comm comm = template.queryForObject(sql, new BeanPropertyRowMapper<Comm>(Comm.class), comm_id);
        System.out.println("新增了一个纪念日数据");
        return comm;
    }

    /*修改纪念日*/
    @Override
    public int amendComm(Comm comm) {
        try {
            String sql = "update commemoration set  comm_name=? , comm_time=? , comm_type=? , time_type=?  where comm_id=? ";
            template.update(sql,comm.getComm_name(),comm.getComm_time(),comm.getComm_type(),comm.getComm_id(),comm.getTime_type());
            System.out.println("修改了id为："+comm.getComm_id()+"的纪念日数据");
            return 1;
        }catch (Exception e) {
            System.out.println("修改纪念日失败");
            return 2;
        }
    }

    /*删除一个纪念日*/
    @Override
    public boolean deleComm(int comm_id) {
        try {
            String sql = "delete from commemoration  where comm_id=? ";
            template.update(sql,comm_id);
            System.out.println("删除了一条纪念日数据");
            return true;
        }catch (Exception e) {
            System.out.println("删除失败");
            return false;
        }
    }

    /*查询所有相册*/
    @Override
    public  List<Images> selectImages() {
        String sql = "select * from images";
        List<Images> images = template.query(sql, new BeanPropertyRowMapper<Images>(Images.class));
        System.out.println("查询了所有相册");
        return images;
    }

    /*根据相册id查询该相册所有分组*/
    @Override
    public List<Img_group> selectImg_group(int images_id) {
        String sql = "select * from img_group where images_id=?";
        List<Img_group> images = template.query(sql, new BeanPropertyRowMapper<Img_group>(Img_group.class),images_id);
        System.out.println("查询了所有相册分组");
        return images;
    }

    /*根据分组id查询所有照片*/
    @Override
    public List<Img> selcetImg(int img_group_id) {
        String sql = "select * from img where img_group_id=?";
        List<Img> imgs = template.query(sql, new BeanPropertyRowMapper<Img>(Img.class),img_group_id);
        System.out.println("查询了所有照片");
        return imgs;
    }

    /*查询相册是否存在*/
    @Override
    public boolean images_Exist(String images_name) {
        String sql = "select count(images_name) from images where images_name=?";
        Long totlal =  template.queryForObject(sql,Long.class,images_name);
        System.out.println("查询了相册是否存在");
        if (totlal==0) {
            return true;
        }else {
            return false;
        }
    }


    /*新增一个相册*/
    @Override
    public int addImages(String images_name) {
        String sql = "insert into images value(null,?,0,'http://xiaoziyan.top/love-app/index-bg.jpg')";
        int req = template.update(sql,images_name);
        System.out.println("新增了一个相册");
        return req;
    }

    /*查询一个相册的图片总数*/
    @Override
    public int selectImgTotal(int images_id) {
        String sql = "select count(img_id) from images,img_group,img where images.images_id=? and images.images_id=img_group.images_id and " +
                "img_group.img_group_id=img.img_group_id ";
        int totlal =  template.queryForObject(sql,Integer.class,images_id);
        System.out.println("查询了照片总数");
        return totlal;
    }

    /*修改相册总数属性*/
    @Override
    public boolean upImg_total( int images_total ,int images_id) {
        try {
            String sql = "update images set  img_total=?  where images_id=? ";
            template.update(sql,images_total,images_id);
            System.out.println("更新了相册照片总数");
            return true;
        }catch (Exception e) {
            System.out.println("更新相册照片总数失败");
            return false;
        }
    }

    /*删除一个相册*/
    @Override
    public boolean deleteImages(int images_id) {
        try {
            String sql = "delete from images  where images_id=? ";
            template.update(sql,images_id);
            System.out.println("删除了一个相册");
            return true;
        }catch (Exception e) {
            System.out.println("删除失败");
            return false;
        }
    }

    /*新增一个照片分组*/
    @Override
    public int addImgGroup(Img_group img_group) {
        String sql = "insert into img_group value(null,?,?,?,?)";
        int req = template.update(sql,img_group.getImages_id(),img_group.getImg_group_time(),img_group.getImg_group_desc(),img_group.getUser_id());
        System.out.println("新增了一个相册分组");

        String sql1 = "select img_group.img_group_id from img_group where img_group_time=?";
        int group_id = template.queryForObject(sql1,Integer.class,img_group.getImg_group_time());
        System.out.println("img_group_id"+group_id);

        return group_id;
    }

    /*查询相册名称*/
    @Override
    public String selectImages_name(int images_id) {
        String sql = "select images_name from images where images_id=?";
        String images_name = template.queryForObject(sql,String.class,images_id);
        return images_name;
    }

    /*新增一张照片*/
    @Override
    public int addImg(int img_group_id, String img_url) {
        String sql = "insert into img value(null,?,?)";
        int req = template.update(sql,img_group_id,img_url);
        System.out.println("新增了一个一张照片");
        return req;
    }

    /*更新相册封面*/
    @Override
    public boolean upimages(int images_id, String images_cover_url) {
        try {
            String sql = "update images set  images_cover_url=?  where images_id=? ";
            template.update(sql,images_cover_url,images_id);
            System.out.println("修改了相册封面");
            return true;
        }catch (Exception e) {
            System.out.println("修改纪念日失败");
            return false;
        }

    }

    /*删除一个分组的所有图片*/
    @Override
    public boolean deleteGroup_img(int img_group_id) {
        try {
            String sql = "delete from img  where img_group_id=? ";
            template.update(sql,img_group_id);
            System.out.println("删除了一个分组的所有照片");
            return true;
        }catch (Exception e) {
            System.out.println("删除失败");
            return false;
        }
    }

    /*删除一个分组*/
    @Override
    public boolean deleteGroup(int img_group_id) {
        try {
            String sql = "delete from img_group  where img_group_id=? ";
            template.update(sql,img_group_id);
            System.out.println("删除了一个分组");
            return true;
        }catch (Exception e) {
            System.out.println("删除失败");
            return false;
        }
    }

    /*查询一个相册第一一张照片*/
    @Override
    public String firstImgUrl(int images_id) {
        String sql = "select img_url from images,img_group,img where images.images_id=? and images.images_id=img_group.images_id " +
                "and img_group.img_group_id=img.img_group_id LIMIT 0,1";
        String img_url = template.queryForObject(sql,String.class,images_id);
        return img_url;

    }

    /*通过openid查询是否登录*/
    @Override
    public String openidLogin(String openid) {
        String sql = "select count(username) from users where openid=?";
        String sql2 = "select username from users where openid=?" ;
        Long totlal =  template.queryForObject(sql,Long.class,openid);
        System.out.println("通过openid查询用户是否存在");
        if (totlal==0) {
            return "3003";
        }else {
            String username = template.queryForObject(sql2,String.class,openid);
            return username;
        }
    }

    /*通过用户名密码登录*/
    @Override
    public String userlogin(String username, String password) {
        String sql = "select count(username) from users where username=? and password=?";
        Long totlal =  template.queryForObject(sql,Long.class,username,password);
        if (totlal==0) {
            return "3003";
        }else {
            return "200";
        }

    }


}
